package com.chixing.ih.module.task.controller;
import com.chixing.ih.json.ResponseData;
import com.chixing.ih.module.task.pojo.DTO.OrderDTO;
import com.chixing.ih.module.task.pojo.DTO.OrderListDTO;
import com.chixing.ih.module.task.pojo.DTO.ProCommentDTO;
import com.chixing.ih.module.task.pojo.DTO.TaskShowDTO;
import com.chixing.ih.module.task.pojo.Order;
import com.chixing.ih.module.task.service.IOrderService;
import com.chixing.ih.module.task.service.ITaskService;
import com.chixing.ih.util.MailUtil;
import com.chixing.ih.util.PageRequest;
import com.chixing.ih.util.RequestIdUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 目的:
 *
 * @author ：zcw
 * @version: 1.0  2020/9/26 16:54
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private IOrderService IOrderService;

    @Autowired
    private ITaskService ITaskService;

    @Autowired
    private MailUtil mailUtil;

    @ApiOperation(value = "获取订单接口", notes = "获取订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "任务id", required = true, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("order/{orderId}")
    public Map<String, Object> getOne(@PathVariable("orderId") Long orderId) {
        Order order = IOrderService.getOne(orderId);
        if (null != order) {
            return ResponseData.REP_GET_SUCCESS(order);
        }
        return ResponseData.REP_INVALID_ERROR();

    }

    @ApiOperation(value = "获取一条订单接口", notes = "获取一条订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("{orderId}")
    public Map<String, Object> getOneOrder(@PathVariable("orderId") Long orderId) {
        OrderDTO oneOrder = IOrderService.getOneOrder(orderId);
        if (null != oneOrder) {
            return ResponseData.REP_GET_SUCCESS(oneOrder);
        }
        return ResponseData.REP_INVALID_ERROR();

    }

    @ApiOperation(value = "查询全部订单接口", notes = "查询全部订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = false, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("orders")
    public Map<String, Object> findAll(PageRequest pageRequest) {
        List<OrderListDTO> list = IOrderService.findAll(pageRequest);
        PageInfo<OrderListDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "我接取的且成功的任务接口", notes = "我接取的且成功的任务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("myTakeOverProgressTaskSuccess")
    public Map<String, Object> findCompletedTaskSuccess(HttpServletRequest request) {
        List<TaskShowDTO> list = IOrderService.findCompletedTaskSuccess(RequestIdUtil.getMyId(request));
        PageInfo<TaskShowDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "我接取的且失败的任务接口", notes = "我接取的且失败的任务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("myTakeOverProgressTaskFailure")
    public Map<String, Object> findCompletedTaskFailure(HttpServletRequest request) {
        List<TaskShowDTO> list = IOrderService.findCompletedTaskFailure(RequestIdUtil.getMyId(request));
        PageInfo<TaskShowDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "我发布的且成功的任务接口", notes = "我发布的且成功的任务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("myReleaseProgressTaskSuccess")
    public Map<String, Object> findCompletedEmploymentSuccess(HttpServletRequest request) {
        List<TaskShowDTO> list =
                IOrderService.findCompletedEmploymentSuccess(RequestIdUtil.getMyId(request));
        PageInfo<TaskShowDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "我发布的且失败的任务接口", notes = "我发布的且失败的任务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("myReleaseProgressTaskFailure")
    public Map<String, Object> findCompletedEmploymentFailure(HttpServletRequest request) {
        List<TaskShowDTO> list =
                IOrderService.findCompletedEmploymentFailure(RequestIdUtil.getMyId(request));
        PageInfo<TaskShowDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "我接取的进行中的任务接口", notes = "我接取的进行中的任务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("myTakeOverProgressTask")
    public Map<String, Object> findProgressTask(HttpServletRequest request) {
        List<TaskShowDTO> list = IOrderService.findProgressTask(RequestIdUtil.getMyId(request));
        PageInfo<TaskShowDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "我发布的进行中的任务接口", notes = "我发布的进行中的任务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("myReleaseProgressTask")
    public Map<String, Object> findProgressEmployment(HttpServletRequest request) {
        List<TaskShowDTO> list = IOrderService.findProgressEmployment(RequestIdUtil.getMyId(request));
        PageInfo<TaskShowDTO> info = new PageInfo<>(list);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "boss聘用接口", notes = "boss聘用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sendTaskUserId", value = "发任务人id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "receiveTaskUserId", value = "接任务人id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 204, message = "操作成功"),
            @ApiResponse(code = 400, message = "操作失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PutMapping("consentApplication")
    public Map<String, Object> consentApplication(@RequestBody Order order) {
        if (IOrderService.consentApplication(order) == 1) {
            return ResponseData.REP_ACCEPTED_NO_HANDLE();
        }
        return ResponseData.REP_NO_ACCESS_ERROR();
    }

    @ApiOperation(value = "申请结算接口", notes = "申请结算")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = false, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 204, message = "申请成功"),
            @ApiResponse(code = 400, message = "申请失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PutMapping("settlement")
    public Map<String, Object> applicationSettlement(@RequestBody Order order) {
        if (IOrderService.applicationSettlement(order) == 1) {
            return ResponseData.REP_ACCEPTED_NO_HANDLE();
        }
        return ResponseData.REP_INVALID_ERROR();
    }

    @ApiOperation(value = "申请退款接口", notes = "申请退款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = false, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 204, message = "申请成功"),
            @ApiResponse(code = 400, message = "申请失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PutMapping("refund")
    public Map<String, Object> applyRefund(@RequestBody Order order) {
        if (IOrderService.applyRefund(order) == 1) {
            return ResponseData.REP_ACCEPTED_NO_HANDLE();
        }
        return ResponseData.REP_INVALID_ERROR();
    }

    @ApiOperation(value = "查询身份接口", notes = "查询身份")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = false, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("identity/{taskId}")
    public Map<String, Object> findOne(@PathVariable("taskId") Long taskId) {
        Order order = IOrderService.findOne(taskId);
        if (null != order) {
            return ResponseData.REP_GET_SUCCESS(order);
        }
        return ResponseData.REP_INVALID_ERROR();
    }

    @ApiOperation(value = "查询待评价订单信息接口", notes = "查询待评价订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("commentOrder/{orderId}")
    public Map<String, Object> getPendingComOrder(@PathVariable("orderId") Long orderId) {
        ProCommentDTO pendingComOrder = IOrderService.getPendingComOrder(orderId);
        return ResponseData.REP_GET_SUCCESS(pendingComOrder);

    }


}
